package com.cd.cms.modules.mid.web;

import com.cd.cms.common.config.Global;
import com.cd.cms.common.persistence.Page;
import com.cd.cms.common.utils.MultipartFileToFileUtils;
import com.cd.cms.common.utils.StringUtils;
import com.cd.cms.common.utils.excel.ImportExcel;
import com.cd.cms.common.web.BaseController;
import com.cd.cms.modules.mid.entity.GoodInfo;
import com.cd.cms.modules.mid.entity.PlatformInfo;
import com.cd.cms.modules.mid.service.GoodInfoService;
import com.cd.cms.modules.mid.service.PlatformInfoService;
import com.cd.cms.modules.sys.entity.Dict;
import com.cd.cms.modules.sys.service.DictService;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 产品Controller
 *
 * @author XueD
 * @version 2016-05-10
 */
@Controller
@RequestMapping(value = "${adminPath}/mid/goodInfo")
public class GoodInfoController extends BaseController {

    @Autowired
    private GoodInfoService goodInfoService;
    @Autowired
	private DictService dictService;
    @Autowired
    private PlatformInfoService platformInfoService;


    @ModelAttribute
    public GoodInfo get(@RequestParam(required = false) String id) {
        GoodInfo entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = goodInfoService.get(id);
        }
        if (entity == null) {
            entity = new GoodInfo();
        }
        return entity;
    }

    @RequiresPermissions("good:goodInfo:view")
    @RequestMapping(value = {"list", ""})
    public String list(GoodInfo goodInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<GoodInfo> page = goodInfoService.findPage(new Page<GoodInfo>(request, response), goodInfo);
        model.addAttribute("page", page);
        return "modules/mid/goodInfoList";
    }

    @RequiresPermissions("good:goodInfo:view")
    @RequestMapping(value = "form")
    public String form(GoodInfo goodInfo, Model model) {
    	GoodInfo goodinfo = goodInfoService.get(goodInfo);
        model.addAttribute("goodInfo", goodinfo);
        PlatformInfo platformInfo= new PlatformInfo();
        platformInfo.setgEngDesc(goodinfo.getgEngDesc());
        List<PlatformInfo> list = platformInfoService.findList(platformInfo);
        model.addAttribute("page", list);
        return "modules/mid/goodInfoForm";
    }

    @RequiresPermissions("good:goodInfo:edit")
    @RequestMapping(value = "save")
    public String save(GoodInfo goodInfo, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, goodInfo)) {
            return form(goodInfo, model);
        }
        Date d = new Date();
        goodInfo.setUpdateDate(d);
        PlatformInfo platformInfo= new PlatformInfo();
        //判断是否平台是否为空
        if(!goodInfo.getPlatformType().equals("0")){
        	platformInfo.setUpdateDate(d);
        	platformInfo.setnPrice(goodInfo.getnPrice());
    		platformInfo.setnSellPrice(goodInfo.getnSellprice());
    		platformInfo.setgEngDesc(goodInfo.getgEngDesc());
        	if(!goodInfo.getPlatformType().equals("1")){
        		//全平台修改价格
        		platformInfo.setPlatformType(goodInfo.getPlatformType());
        	}
        }
        goodInfoService.updatePrice(goodInfo, platformInfo);
        addMessage(redirectAttributes, "修改产品成功");
        return "redirect:" + Global.getAdminPath() + "/mid/goodInfo/?repage";
    }

    @RequiresPermissions("good:goodInfo:edit")
    @RequestMapping(value = "delete")
    public String delete(GoodInfo goodInfo, RedirectAttributes redirectAttributes) {
        goodInfoService.delete(goodInfo);
        addMessage(redirectAttributes, "删除产品成功");
        return "redirect:" + Global.getAdminPath() + "/mid/goodInfo/?repage";
    }

    @RequiresPermissions("good:goodInfo:edit")
    @RequestMapping(value = "import", method = RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes, HttpServletRequest request,GoodInfo goodInfo) {
        //TODO:ozy 上传文件写到这里
        try {
        	//判断表
            String path = MultipartFileToFileUtils.multipartFileToFile(file, request);

            List<GoodInfo> list = this.readExcelToList(path,goodInfo.gettType());
            Dict dict = new Dict();
            dict.setType("sys_user_type");
            List<Dict> dictlist = dictService.findList(dict);
            GoodInfo paramInfo = new GoodInfo();
            paramInfo.setIsvalid("1");
            List<GoodInfo>  goodInfos = goodInfoService.findList(paramInfo);
            PlatformInfo platformInfo = new PlatformInfo();
            platformInfo.setIsvalid("1");
            List<PlatformInfo> platformInfos = platformInfoService.findList(platformInfo);
            goodInfoService.insetAll(list, goodInfo.getPlatformType(), dictlist,goodInfos,platformInfos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:" + adminPath + "/mid/goodInfo/list?repage";
    }

    private List<GoodInfo> readExcelToList(String filePath,String product_type){
        List<GoodInfo> list = new ArrayList<>();
        GoodInfo goodInfo ;
        try {
            ImportExcel ei = new ImportExcel(filePath, 0);
            for (int i = ei.getDataRowNum(); i <=ei.getLastDataRowNum(); i++) {
                Row row = ei.getRow(i);
                goodInfo = new GoodInfo();
                if("bjp".equals(product_type)){
                    if(row.getCell(0)!=null){
                        row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(0).getStringCellValue().length()>0){
                        	 goodInfo.setgProductBrand(row.getCell(0).getStringCellValue());
                        }
                    }if(row.getCell(1)!=null){
                        row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(1).getStringCellValue().length()>0){
                        	 goodInfo.setgBarCode(row.getCell(1).getStringCellValue());
                        }
                       
                    }if(row.getCell(2)!=null){
                        row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(2).getStringCellValue().length()>0){
                        	 goodInfo.setgSpecifications(row.getCell(2).getStringCellValue());
                        }
                    }if(row.getCell(3)!=null){
                        row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(3).getStringCellValue().length()>0){
                        	goodInfo.setgEngDesc(row.getCell(3).getStringCellValue());
                        }
                    }if(row.getCell(4)!=null){
                        row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(4).getStringCellValue().length()>0){
                        	 goodInfo.setgChineseDesc(row.getCell(4).getStringCellValue());
                        }
                    }
                    if(row.getCell(5)!=null){
                        row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(5).getStringCellValue().length()>0){
                        	goodInfo.setWeight(row.getCell(5).getStringCellValue());
                        }
                    }
                    if(row.getCell(6)!=null){
                        row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(6).getStringCellValue().length()>0){
                        	goodInfo.setSingleWLimit(row.getCell(6).getStringCellValue());
                        }
                    }
                    if(row.getCell(7)!=null){
                        row.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(7).getStringCellValue().length()>0){
                        	 goodInfo.setMixWLimit(row.getCell(7).getStringCellValue());
                        }
                    }
                    if(row.getCell(8)!=null){
                        row.getCell(8).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(8).getStringCellValue().length()>0){
                        	 goodInfo.setnPrice(row.getCell(8).getStringCellValue());
                        }
                    }
                    if(row.getCell(9)!=null){
                        row.getCell(9).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(9).getStringCellValue().length()>0){
                        	 goodInfo.setnSellprice(row.getCell(9).getStringCellValue());
                        }
                       
                    }
                    goodInfo.settType("bjp");
                }else if("nf".equals(product_type)){
                    if(row.getCell(0)!=null){
                        row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(0).getStringCellValue().length()>0){
                        	goodInfo.setgProductBrand(row.getCell(0).getStringCellValue());
                        }
                    }
                    if(row.getCell(1)!=null){
                        row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(1).getStringCellValue().length()>0){
                        	goodInfo.setgEngDesc(row.getCell(1).getStringCellValue());
                        }
                    }
                    if(row.getCell(2)!=null){
                        row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(2).getStringCellValue().length()>0){
                        	goodInfo.setgChineseDesc(row.getCell(2).getStringCellValue());
                        }
                    }
                    goodInfo.settType("nf");
                }else if("Jurlique".equals(product_type)){
                    if(row.getCell(1)!=null){
                        row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(1).getStringCellValue().length()>0){
                        	goodInfo.setgProductBrand(row.getCell(1).getStringCellValue());
                        }
                    }
                    if(row.getCell(2)!=null){
                        row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(2).getStringCellValue().length()>0){
                        	goodInfo.setgEngDesc(row.getCell(2).getStringCellValue());
                        }
                    }
                    if(row.getCell(3)!=null){
                        row.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(3).getStringCellValue().length()>0){
                        	goodInfo.setgChineseDesc(row.getCell(3).getStringCellValue());
                        }
                    }
                    if(row.getCell(4)!=null){
                        row.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(4).getStringCellValue().length()>0){
                        	goodInfo.setgCapacity(row.getCell(4).getStringCellValue());                        	
                        }
                        
                    }
                    if(row.getCell(5)!=null){
                        row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(5).getStringCellValue().length()>0){
                        	goodInfo.setnPrice(row.getCell(5).getStringCellValue());
                        }
                    }
                    if(row.getCell(6)!=null){
                        row.getCell(6).setCellType(Cell.CELL_TYPE_STRING);
                        if(row.getCell(6).getStringCellValue().length()>0){
                        	goodInfo.setnSellprice(row.getCell(6).getStringCellValue());
                        }
                    }
                    goodInfo.settType("Jurlique");
                }
                list.add(goodInfo);
            }
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
}